import * as  React from "react";
import { Picker } from '@tarojs/components'
import moment from 'moment'
import type { DateTimePickerProps } from '../../types/DateTimePicker'



type DsProp = number[]

export const DateTimePicker:
  React.FC<DateTimePickerProps>

  = (props) => {

    const {
      defaultDateTimeFormat = 'YYYY-MM-DD HH:mm',
      defaultDateTime = moment()
    } = props

    const initDataList = (defaultMoment: moment.Moment = moment()): Array<DsProp> => {
      let year = Number(defaultMoment.format('YYYY'))
      let daysInMonth = defaultMoment.daysInMonth()
      let years: any[] = Array.from(({ length: 4 }), (_, k) => (year + k - 1) + '年')
      let months: any[] = Array.from(({ length: 12 }), (_, k) => (k + 1) + '月')

      let days: any[] = Array.from(({ length: daysInMonth }), (_, k) => (k + 1) + '日')
      let hours: any[] = Array.from(({ length: 24 }), (_, k) => (k) + '时')
      let minutes: any[] = Array.from(({ length: 60 }), (_, k) => (k) + '分')
      let ds = [years, months, days, hours, minutes]

      // console.log('initDataList ds', ds)
      return ds
    }


    const initIndex = (defaultMoment: moment.Moment = moment()): number[] => {
      let yearIndex = 1
      let monthIndex = Number(defaultMoment.format('MM')) - 1
      let dayIndex = Number(defaultMoment.format('DD')) - 1
      let hourIndex = Number(defaultMoment.format('HH'))
      let minuteIndex = Number(defaultMoment.format('mm'))
      let ds = [yearIndex, monthIndex, dayIndex, hourIndex, minuteIndex]

      // //console.log('initIndex ds', ds)
      return ds
    }


    // let defaultDateTime = moment()

    // if (props.defaultDateTime) {
    //   switch (typeof props.defaultDateTime) {
    //     case 'string':
    //       defaultDateTime = moment(props.defaultDateTime, defaultDateTimeFormat)
    //       break;
    //     case 'object': {
    //       if (moment.isMoment(props.defaultDateTime)) {
    //         defaultDateTime = props.defaultDateTime
    //       } else if (props.defaultDateTime instanceof Date) {
    //         defaultDateTime = moment.utc(props.defaultDateTime)
    //       }
    //     }
    //     default: break;
    //   }
    // }

    const [state, dispatch] = React.useReducer((state, action) => ({ ...state, ...action }), {
      dataList: initDataList(moment()),
      index: initIndex(moment())
    })



    React.useEffect(() => {
      let _defaultDateTime = moment()
      switch (typeof props.defaultDateTime) {
        case 'string':
          _defaultDateTime = moment(props.defaultDateTime, defaultDateTimeFormat)
          dispatch({
            dataList: initDataList(_defaultDateTime),
            index: initIndex(_defaultDateTime)
          })
          break;
        case 'object': {
          if (moment.isMoment(props.defaultDateTime)) {
            // _defaultDateTime = props.defaultDateTime
          } else if (props.defaultDateTime instanceof Date) {
            _defaultDateTime = moment.utc(props.defaultDateTime)
            dispatch({
              dataList: initDataList(_defaultDateTime),
              index: initIndex(_defaultDateTime)
            })
          }
        }
        default: break;
      }

    }, [defaultDateTime])

    return React.createElement(
      Picker,
      {
        mode: 'multiSelector',
        range: state.dataList,
        value: state.index,
        onChange: e => {

          let year = state.dataList[0][e.detail.value[0]]
          let month = state.dataList[1][e.detail.value[1]]
          let day = state.dataList[2][e.detail.value[2]]
          let hour = state.dataList[3][e.detail.value[3]]
          let minte = state.dataList[4][e.detail.value[4]]
          //console.log('onChange', e.detail.value,year,month,day,hour,minte)
          let dateTime = moment(`${year}-${month}-${day} ${hour}:${minte}`, 'YYYY-M-D H:m').format(defaultDateTimeFormat)
          if (props.onValueChange) {
            props.onValueChange(dateTime)
          }
        },
        onColumnChange: e => {
          /**{column: 1, value: 9} */
          //console.log('onColumnChange', e.detail, state)
          state.index[e.detail.column] = e.detail.value
          if (new Set([0, 1]).has(e.detail.column)) {
            let year = state.dataList[0][state.index[0]]
            let month = state.dataList[1][state.index[1]]
            let daysInMonth = moment(`${year}${month}`, 'YYYY年M月').daysInMonth()
            state.dataList[2] = Array.from(({ length: daysInMonth }), (v, k) => (k + 1) + '日')
            let dataList = state.dataList.map(i => i)
            dispatch({ dataList })
          }
        },
      },
      props.children
    )
  }